package com.yuqing.sentimentapp.home.presenter.impl;

import com.yuqing.sentimentapp.base.BaseObserver;
import com.yuqing.sentimentapp.home.bean.EarlyWarningDataBean;
import com.yuqing.sentimentapp.home.fragment.EarlyWarningFragment;
import com.yuqing.sentimentapp.home.presenter.EarlyWarningPresenter;
import com.yuqing.sentimentapp.module.ModuleManager;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by ShenHe on 2019/3/14.
 */

public class EarlyWarningPresenterImpl implements EarlyWarningPresenter {
    private EarlyWarningFragment earlyWarningFragment;

    public EarlyWarningPresenterImpl(EarlyWarningFragment earlyWarningFragment) {
        this.earlyWarningFragment = earlyWarningFragment;
    }

    @Override
    public void loadEarlyWarningData(int page, int pageSize, boolean isRefresh) {
        ModuleManager.getInstance().loadEarlyWarningResult(earlyWarningFragment.getContext(), page, pageSize)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<EarlyWarningDataBean>() {
                    @Override
                    protected void hideDialog() {
                        earlyWarningFragment.stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        earlyWarningFragment.startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(EarlyWarningDataBean bean) {
                        if (checkContext()) return;
                        earlyWarningFragment.returnEaylyWarningResult(bean, isRefresh);
                    }

                    @Override
                    protected void onFailure(String message) {
                        earlyWarningFragment.onFailure(message);
                    }

                });
    }

    private boolean checkContext() {
        if (earlyWarningFragment.getActivity() == null || earlyWarningFragment.getActivity().isFinishing() || earlyWarningFragment.getActivity().isDestroyed())
            return true;
        return false;
    }
}
